package com.zyj.jetpackdemo.composes

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.drawscope.withTransform

@Composable
fun DrawLine() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        val canvasWidth = size.width
        val canvasHeight = size.height
        drawLine(
            start = Offset(x = canvasWidth, y = 0f),
            end = Offset(x = 0f, y = canvasHeight),
            color = Color.Blue,
            strokeWidth = 5F
        )
    }
}

@Composable
fun DrawCircle() {
    Column() {

        Canvas(modifier = Modifier.fillMaxWidth().fillMaxHeight(0.5f)) {
            val canvasWidth = size.width
            val canvasHeight = size.height
            drawCircle(
                color = Color.Blue,
                center = Offset(x = canvasWidth / 2, y = canvasHeight / 4),
                radius = size.minDimension / 4
            )
        }

        Canvas(modifier = Modifier.fillMaxWidth().fillMaxHeight(0.5f)) {
            val canvasWidth = size.width
            val canvasHeight = size.height
            drawCircle(
                color = Color.Blue,
                center = Offset(x = canvasWidth / 2, y = canvasHeight / 4),
                radius = size.minDimension / 2,
                style = Stroke(width = 1.0f)
            )
        }
    }

}

@Composable
fun DrawRect() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        val canvasQuadrantSize = size / 2F
        drawRect(
            color = Color.Black,
            size = canvasQuadrantSize
        )
    }
}

@Composable
fun DrawInset() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        val canvasQuadrantSize = size / 2F
        inset(50F, 30F) {
            drawRect(
                color = Color.Gray,
                size = canvasQuadrantSize
            )
        }
    }
}

@Composable
fun TransformationOfRotate() {

    Canvas(modifier = Modifier.fillMaxSize()) {
        rotate(degrees = 45F) {
            val canvasSize = size
            val canvasWidth = size.width
            val canvasHeight = size.height
            drawRect(
                color = Color.Gray,
                topLeft = Offset(x = canvasWidth / 3F, y = canvasHeight / 3F),
                size = canvasSize / 3F
            )
        }
    }

}

@Composable
fun DrawWithTransformFunction() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        val canvasSize = size
        val canvasWidth = size.width
        val canvasHeight = size.height
        withTransform({
            translate(left = canvasWidth / 5F)
            rotate(degrees = 45F)
        }) {
            drawRect(
                color = Color.Gray,
                topLeft = Offset(x = canvasWidth / 3F, y = canvasHeight / 3F),
                size = canvasSize / 3F
            )
        }
    }
}